﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class WebCamManager : MonoBehaviour
{

    WebCamTexture webcamTexture;
    WebCamTexture webcamTexture2;
    private Text _text;
    RawImage ri;
    Image image;
    RawImage ri2;
    // Use this for initialization
    void Start()
    {
        _text = GameObject.Find("LogText").GetComponent<Text>();
        _text.text = "Begin";

        ri = GetComponent<RawImage>();
       // ri2 = GameObject.Find("RawImage2").GetComponent<RawImage>();
        image = GameObject.Find("Image").GetComponent<Image>();

        GameObject.Find("Button (0)").GetComponent<Button>().onClick.AddListener(delegate ()
        {
            // 调用摄像头
            StartCoroutine(StartCamare());
        });

        GameObject.Find("Button (1)").GetComponent<Button>().onClick.AddListener(delegate ()
        {
            // 调用摄像头
            StartCoroutine(GetTexture());
        });

        //AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        //AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        //int res = jo.Call<int>("add", 56, 100);
        //Debug.Log(res);
    }

    public bool CheckA()
    {
        bool success = false;
        return success;
    }

    IEnumerator StartCamare()
    {
        
        Debug.Log("尝试获取权限");
        _text.text = "尝试获取权限";

        //yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);

        //if (!Application.HasUserAuthorization(UserAuthorization.WebCam))
        //{
        //    _text.text = "么得权限";
        //    Debug.Log("么得权限");
        //    yield break;
        //}
        //else
        //{
        //    Debug.Log("有权限");
        //}

        //if (!CheckA())
        //{
        //    yield break;
        //}

        webcamTexture = new WebCamTexture(300,300);
        webcamTexture2 = new WebCamTexture(300, 300);

        if (webcamTexture.videoVerticallyMirrored)
        {
            Debug.Log("videoVerticallyMirrored为true");

        }
        else
        {
            Debug.Log("videoVerticallyMirrored为false");

        }

        if (WebCamTexture.devices.Length == 0)
        {
            _text.text = "么得相机";
            yield break;
        }

        //调用前色相头
        for (int i = 0; i < WebCamTexture.devices.Length; i++)
        {
            webcamTexture.deviceName = WebCamTexture.devices[i].name;
            if (WebCamTexture.devices[i].isFrontFacing)
            {
                webcamTexture.deviceName = WebCamTexture.devices[i].name;
                break;
            }
        }

        //调用前色相头
        //for (int i = 0; i < WebCamTexture.devices.Length; i++)
        //{
        //    webcamTexture2.deviceName = WebCamTexture.devices[i].name;
        //    if (WebCamTexture.devices[i].isFrontFacing)
        //    {
        //        webcamTexture2.deviceName = WebCamTexture.devices[i].name;
        //        break;
        //    }
        //}

        //Renderer renderer = GetComponent<Renderer>();
        //renderer.material.mainTexture = webcamTexture;

        ri.texture = webcamTexture;
        //ri2.texture = webcamTexture2;
        
        webcamTexture.Play();
        webcamTexture2.Play();
        _text.text = "开始拍了";

        //调整角度
        int orient = -webcamTexture.videoRotationAngle;
        ri.rectTransform.localEulerAngles = new Vector3(0, 0, orient);

        int orient2 = -webcamTexture2.videoRotationAngle;
        //ri2.rectTransform.localEulerAngles = new Vector3(0, 0, orient);

        //如果要镜像需要改变scaleY
        float scaleY = webcamTexture.videoVerticallyMirrored ? 1f : -1f; // Find if the camera is mirrored or not  
        ri.rectTransform.localScale = new Vector3(1f, scaleY, 1f); // Swap the mirrored camera 

        Debug.Log("videoRotationAngle:" + webcamTexture.videoRotationAngle);

        //ri.rectTransform.sizeDelta = new Vector2(ri.rectTransform.sizeDelta.y, ri.rectTransform.sizeDelta.x);

        if (webcamTexture.videoVerticallyMirrored)
        {
            Debug.Log("videoVerticallyMirrored为true");
        }
        else
        {
            Debug.Log("videoVerticallyMirrored为false");
        }

        Debug.Log("Required width:" + webcamTexture.requestedWidth);
        Debug.Log("Required height:" + webcamTexture.requestedHeight);
    }

    IEnumerator GetTexture()
    {
        //webcamTexture.Pause();
        yield return new WaitForEndOfFrame();


        Texture2D mTexture = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.ARGB32, false);
        //试试强转
        //Texture2D t = (Texture2D)ri.texture;
        mTexture.SetPixels(webcamTexture.GetPixels(0, 0, webcamTexture.width, webcamTexture.height));
        mTexture.Apply();

        image.rectTransform.sizeDelta = new Vector2(webcamTexture.width, webcamTexture.height);
        image.sprite = Sprite.Create(mTexture, new Rect(0, 0, webcamTexture.width, webcamTexture.height), Vector2.zero);

        int rotation = -webcamTexture.videoRotationAngle;
        image.rectTransform.localEulerAngles = new Vector3(0, 0, rotation);


        //webcamTexture.Play();

        Debug.Log((int)ri.rectTransform.sizeDelta.x + "-" + (int)ri.rectTransform.sizeDelta.y);
        Debug.Log("webcamTexture.width:" + webcamTexture.width);
        Debug.Log("webcamTexture.height:" + webcamTexture.height);
        yield return null;
    }

}